/* eslint-disable no-prototype-builtins */
import _ from 'lodash';

export function findAntFormItemParent(element: HTMLElement) {
  let parent = element.parentElement;
  while (parent) {
    if (parent.classList?.contains('ant-form-item')) {
      return parent;
    }
    parent = parent.parentElement;
  }
  return null;
}

export const withBatchUpdateProp = (setProp: (prop: any) => void) => {
  return (values: any) => {
    setProp((prop: any) => {
      const fn = (prop: any, values: any) => {
        Object.keys(values).forEach((key) => {
          if (_.isPlainObject(values[key]) && prop.hasOwnProperty(key)) {
            fn(prop[key], values[key]);
          } else {
            prop[key] = values[key];
          }
        });
      };
      fn(prop, values);
    });
  };
};
